/*
Basic HTTP interface for the SMTP server.

Accepts POST requests for sending an SMTP message.
The POST data should have fields: from, to, subject and body

Accepts GET requests for searching the logged messages.
*/
var helper = require("./helper");
var http = require('http');
var SmtpClient = require('./SmtpClient');

// wrapper function allows for easy debugging (using return)
(function main() {

    // eases development process
    helper.exitOnKeyPress();
    // the port on which the http server is listening
    var port = 8800;
    http.createServer(function (req, res) {
        switch (req.method) {
            case "POST":
                helper.processPost(req, res, function () {
                    console.log(req.post);
                    var smtpc = new SmtpClient(25);
                    //smtpc.sendMessage(req.post);
                    // TODO: store message
                });
                break;
            case "GET":
                // TODO: process search
                break;
            default: // other HTTP verbs not implemented
        }
        res.writeHead(200, "OK", { 'Content-Type': 'text/plain' });
        res.end();
    }).listen(port);
    console.log("HTTP server is listening on port " + port);

}())